<template>
  <div>
    <!--面包屑-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>XXX</el-breadcrumb-item>
      <el-breadcrumb-item>XXX</el-breadcrumb-item>
    </el-breadcrumb>
    <!--数据列表主工作区-->
    <el-card>
      <!--搜索框和添加按钮-->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入搜索内容" class="input-with-select" v-model="pageReqParams.query">
            <el-button slot="append" icon="el-icon-search" @click="handleSearchButtonClick()"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="handleAddButtonClick()">添加</el-button>
        </el-col>
      </el-row>

      <!--用户列表-->
      <el-table
        :data="pageInfo.list"
        border
        stripe
        style="width: 100%">

        <el-table-column
          prop="id"
          label="ID"
          width="70">
        </el-table-column>

        <el-table-column label="操作" width="250px">
          <template slot-scope="scope">
            <el-tooltip effect="dark" content="修改" placement="top" :enterable="false">
              <el-button type="primary" icon="el-icon-edit"
                         @click="handleModifyButtonClick(scope.row)"></el-button>
            </el-tooltip>

            <el-tooltip effect="dark" content="删除" placement="top" :enterable="false">
              <el-button type="danger" icon="el-icon-delete"
                         @click="handleDeleteButtonClick(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!--数据列表分页-->
      <el-pagination
        @size-change="handlePageSizeChange"
        @current-change="handleCurrentPageNumberChange"
        :current-page="pageInfo.pageNum"
        :page-sizes="pageSizes"
        :page-size="pageInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageInfo.total">
      </el-pagination>
    </el-card>
    <!--添加对话框-->
    <el-dialog title="添加"
               :center="true"
               :visible.sync="addDialogVisible" width="50%">


      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancelAddButtonClick()">取 消</el-button>
        <el-button type="primary" @click="handleConfirmAddButtonClick()">确 定</el-button>
      </div>

    </el-dialog>
    <!--修改对话框-->
    <el-dialog title="修改"
               :center="true"
               :visible.sync="modifyDialogVisible" width="50%">



      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancelModifyButtonClick()">取 消</el-button>
        <el-button type="primary" @click="handleConfirmModifyButtonClick()">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

  export default {
    name: "CrudTableTemplate",
    data() {
      return {

        // 分页请求时要携带的参数
        pageReqParams: {
          query: '',
          pageNum: 1,
          pageSize: 5
        },
        // 分页信息
        pageInfo: {
          pageNum: 0,
          pageSize: 0,
          total: 0,
          totalPage: 0,
          list: []
        },
        // 分页的大小常量列表
        pageSizes: [5,10,20, 30, 50, 100],
        // 添加对话框显示开关
        addDialogVisible: false,
        // 修改对话框显示开关
        modifyDialogVisible: false,
        // 添加对话框中的表单数据
        addForm: {
        },
        // 表单的宽度变量
        formLabelWidth: 500,

        currentModifyingData: {}
      }
    },


    created() {
      this.getPageFunction();
    },
    methods: {

      //打开修改对话框
      handleModifyButtonClick(row) {
        // TODO 先去请求原始数据，然后显示对话框
        this.modifyDialogVisible = true;
      },

      // 确认修改
      handleConfirmModifyButtonClick() {
        this.modifyDialogVisible = false;
        //TODO 发起修改请求
      },

      // 删除
      async handleDeleteButtonClick(row) {

        const res = await this.$confirm('确定要删除?', "提示");
        if (res == 'confirm') {
          // TODO 发起删除请求
        } else {
          this.$message.info("已取消删除");
        }
      },

      //确认添加
      handleConfirmAddButtonClick() {
        //TODO 发起添加请求
      },

      //点击搜索
      handleSearchButtonClick() {
        this.getPageFunction();
      },

      //页大小改变
      handlePageSizeChange(size) {
        this.pageInfo.pageSize = size;
        this.pageReqParams.pageSize = size;
        this.getPageFunction();
      },

      //页码改变
      handleCurrentPageNumberChange(pageNum) {
        this.pageReqParams.pageNum = pageNum;
        this.getPageFunction();
      },

      //获取一页数据
      getPageFunction() {
        //TODO 发起分页请求
      },

      //弹出添加对话框
      handleAddButtonClick() {
        this.addDialogVisible = true;
      },

      //取消添加
      handleCancelAddButtonClick() {
        this.addDialogVisible = false;
      },

      //取消修改
      handleCancelModifyButtonClick() {
        this.modifyDialogVisible = false;
      }
    }

  }
</script>

<style scoped>

  .el-pagination {
    margin: 5px;
  }

</style>
